package cn.lg.soar.system.tenant.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.core.util.easyexcel.ExcelUtil;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.tenant.entity.Tenant;
import cn.lg.soar.system.tenant.service.ITenantService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;

/**
 * Tenant控制器
 * 租户
 * @author luguoxiang
 * @since 2022-04-30
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ConditionalOnProperty(name = "lg.database.enabled-tenant", havingValue = "true")
@Validated
@Api(tags = "租户")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/tenant")
public class TenantController {

    @Autowired
    private ITenantService service;

    private final static String SUBJECT = "租户";

    @Log(value = "新增" + SUBJECT, data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @CreateMapping
    public HttpResult<Serializable> create(@RequestBody @Valid Tenant model) {
        AssertUtil.isTrue(service.save(model), "创建失败");
        return HttpResult.success("创建成功");
    }

    @Log(value = "修改" + SUBJECT, data = "#p0", dataId = "#p0.id")
    @UpdateMapping
    public HttpResult<Void> update(@RequestBody @Valid Tenant model) {
        AssertUtil.isTrue(service.updateById(model), "更新失败");
        return HttpResult.success("更新成功");
    }

    @Log(value = "删除" + SUBJECT, data = "#p0", type = Log.Type.TEXT)
    @RemoveMapping
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        AssertUtil.isTrue(service.removeByIds(ids), "删除失败");
        return HttpResult.success("删除成功");
    }

    @DetailMapping
    public HttpResult<Tenant> detail(@Id Long id) {
        Tenant data = service.getById(id);
        AssertUtil.notNull(data, "数据不存在");
        return HttpResult.data(data);
    }

    @QueryMapping
    public HttpResult<PageResult<Tenant>> query(PageQuery pageQuery, QueryParams params){
        PageResult<Tenant> query = new SqueryWapper<>(Tenant.class).query(service, pageQuery, params);
        return HttpResult.data(query);
    }

    @Log(value = "导出" + SUBJECT, data = "#p0")
    @ExportMapping
    public void exports(String filename, QueryParams params, HttpServletResponse resp){
        QueryWrapper<Tenant> wrapper = new SqueryWapper<>(Tenant.class).listWrapper(params);
        List<Tenant> list = service.list(wrapper);
        ExcelUtil.exportExcel(filename, list, resp);
    }

    @Log(value = "导入" + SUBJECT)
    @ImportMapping
    public HttpResult<Void> imports(HttpServletRequest req){
        List<Tenant> tenants = ExcelUtil.importExcel(req, Tenant.class);
        AssertUtil.isTrue(service.saveBatch(tenants), "导入失败");
        return HttpResult.success("导入成功");
    }
}

